package br.unisinos.pdm.activity.component;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * Componente para representar pergunta de multipla escolha 
 */
public class PerguntaRadioGroup extends LinearLayout implements IPerguntaComponent {

	private List<RadioButton> radioButtonList = null;
	private String pergunta = null;
	
	public PerguntaRadioGroup(Context context, List<String> list, String pergunta, String value) {
		super(context);
		this.pergunta = pergunta;
		init(list, value);
	}
	
	public PerguntaRadioGroup(Context context, AttributeSet attrs, List<String> list, String pergunta, String value) {
		super(context, attrs);
		this.pergunta = pergunta;
		init(list, value);
	}
	
	private void init(List<String> list, String value) {
		setOrientation(LinearLayout.VERTICAL);
		
		TextView textView = new TextView(getContext());
    	textView.setText(pergunta);
    	textView.setPadding(90, 0, 90, 0);
    	addView(textView);
    	
        RadioGroup radioGroup = new RadioGroup(getContext());
        
        radioButtonList = new ArrayList<RadioButton>();
        for (String string : list) {
        	RadioButton radioButton = new RadioButton(getContext());
        	radioButton.setText(string);
        	radioButtonList.add(radioButton);
        	radioGroup.addView(radioButton);
        	if(value != null){
        		if(value.equalsIgnoreCase(string)){
        			radioButton.setChecked(true);
        		}
        	}
		}
		radioGroup.setPadding(90, 0, 90, 0);
		addView(radioGroup);
	}
	
	
	@Override
	public String getValue() {
		
		if(radioButtonList == null){
			return null;
		}
		
		for (RadioButton radioButton : radioButtonList) {
			if(radioButton.isChecked()){
				return radioButton.getText().toString();
			}
		}
		
		return null;
	}
	
	@Override
	public String getPergunta() {
		return pergunta;
	}

}
